# 工作流调用请求类
class invoke_workflow_request:
    def __init__(self, workflow_name, input_data, execution_name=None):
        """
        初始化工作流调用请求
        :param workflow_name: 工作流名称
        :param input_data: 输入参数，JSON字符串
        :param execution_name: 执行名称，可选
        """
        self.workflow_name = workflow_name
        self.input = input_data
        self.execution_name = execution_name
    
    def to_json(self):
        """
        转换为JSON格式
        """
        data = {
            "workflowName": self.workflow_name,
            "input": self.input
        }
        if self.execution_name:
            data["executionName"] = self.execution_name
        return json.dumps(data)

# 工作流服务类
class workflow_service:
    def __init__(self, client):
        """
        初始化工作流服务
        :param client: 客户端实例
        """
        self.client = client
        self.endpoint = client.config.endpoint

    def invoke_workflow(self, region, request):
        """
        调用工作流
        :param region: 区域ID
        :param request: 工作流调用请求
        :return: 响应结果
        """
        if not region:
            raise ValueError('region is required')
        if not request or not request.workflow_name:
            raise ValueError('workflow_name is required')
        
        url = "openapi/v1/execution/start"
        
        headers = {
            'Content-Type': 'application/json',
            'regionId': region
        }
        
        config = {
            'method': self.client.config.method,
            'url': url,
            'headers': headers,
            'data': {
                'workflowName': request.workflow_name,
                'input': request.input,
                'executionName': request.execution_name
            }
        }
        
        res = self.client.do_request(config)
        if isinstance(res, ValueError):
            print(f'invoke workflow err: {res}')
            return res
        else:
            print(f'invoke workflow success: {res}')
            return res